package design.graduation.novelsystem.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ShearCaptcha;
import design.graduation.novelsystem.model.dto.DataGridView;
import design.graduation.novelsystem.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @author: 洪少尉
 * @Date: 2020/4/14 01:35
 * @Description: 工具接口请求
 */
@Controller
@RequestMapping("util")
public class UtilController {
	@Autowired
	private StringRedisTemplate redisTemplate;
	@Autowired
	private AdminService adminService;

	/**
	 * 生成验证码
	 */
	@RequestMapping("captcha")
	public void captcha(HttpServletResponse response, String codeKey) throws IOException {
		ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(100, 38, 4, 4);
		String code = captcha.getCode();
		ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
		opsForValue.set(codeKey,code);
		opsForValue.getOperations().expire(codeKey,60, TimeUnit.SECONDS);
		captcha.write(response.getOutputStream());
	}
	/**
	 * 上传头像图片
	 */
	@PostMapping("uploadFile")
	@ResponseBody
	public Object uploadFile(MultipartFile mf){
		return new DataGridView(this.adminService.updateAdminHeadImage(mf));
	}
}
